$(function () {
    /**
     加载下达任务
     **/
    function getTask() {
        $.ajax({
            // url: '../json/getTask.json',
            url: baseUrl + 'getTask',
            type: 'get',
            // dataType: 'default: Intelligent Guess (Other values: xml, json, script, or html)',
            // data: { method: 'getTask' },
        })
            .done(function (data) {
                console.log("getTask");
                var data = data.data;
                var html = "";

                for (var i = 0; i < data.length; i++) {
                    html += `<tr>
                            <td>${data[i].taskStyle}</td>
                            <td>${data[i].batch}</td>
                            <td>${data[i].productlineNum}</td>
                            <td>${data[i].planQuantity}</td>
                            <td>${data[i].remark}</td>`;
                    if (data[i].taskStatus == 1) {
                        html += `<td>任务已完成</td>;
                    		 </tr>`;
                    } else if (data[i].taskStatus == 0) {
                        html += `<td class="last"> <button data-taskStyle="${data[i].taskStyle}" data-batch="${data[i].batch}" data-productlineNum="${data[i].productlineNum}" class="delTask">删除任务</button> <button data-taskStyle="${data[i].taskStyle}" data-batch="${data[i].batch}" data-productlineNum="${data[i].productlineNum}" class="comTask">任务完成</button></td>
                       </tr>`;
                    }
                }

                $(html).appendTo('#table');
            })
            .fail(function () {
                console.log("error");
            })
    }

    getTask();

    $("#addTask").click(function(event) {
        /* Act on the event */
        var html = "";
        html += `<tr>
                        <td><select class="taskStyle"></select></td>
                        <td><input type="text" class="batch"></td>
                        <td><input type="text" class="productlineNum"></td>
                        <td><input type="text" class="planQuantity"></td>
                        <td><input type="text" class="remark"></td>
                        <td class="last"><button class="addTask">下达任务</button></td>
                    </tr>`;
        $(html).appendTo('#table');
        $.ajax({
            url: baseUrl+"getProcess",
            type: 'get',
        }).done(function (data) {
            console.log("getProcess");
            var data = data.data;
            var html = "";
            for (var i = 0; i < data.length; i++) {
                html += `<option value="${data[i]}">${data[i]}</option>`;
            }

            $(html).appendTo('.taskStyle');
        })
    });

    /**
     添加任务
     **/
    function addTask(taskStyle, batch, productlineNum, planQuantity, remark, taskStatus) {
        $.ajax({
            // url: '../json/getTask.json',
            url: baseUrl + 'addTask',
            type: 'get',
            // dataType: 'default: Intelligent Guess (Other values: xml, json, script, or html)',
            data: {
                taskStyle: taskStyle,
                batch: batch,
                productlineNum: productlineNum,
                planQuantity: planQuantity,
                remark: remark,
                taskStatus: taskStatus
            },
        })
            .done(function (data) {
                console.log("addTask");
                // var data = data.data;
                // var html = "";
                if (data.code == 0) {
                    layer.open({
                        title: '信息',
                        content: '任务添加成功！',
                        yes: function (index, layero) {
                            //按钮【按钮一】的回调
                            layer.close(index);
                            window.location.reload();
                        }
                    });
                } else if (data.code == 1) {
                    layer.open({
                        title: '信息',
                        content: '任务添加失败！',
                        yes: function (index, layero) {
                            //按钮【按钮一】的回调
                            layer.close(index);
                            window.location.reload();
                        }
                    });
                }
            })
            .fail(function () {
                console.log("error");
                layer.open({
                    title: '信息'
                    , content: '任务添加失败！'
                });
            })
    }

    $("body").on('click', 'table .addTask', function (event) {
        event.preventDefault();
        var taskStyle = $(".taskStyle").val();
        var batch = $(".batch").val();
        var productlineNum = Number($(".productlineNum").val());
        var planQuantity = Number($(".planQuantity").val());
        var remark = $(".remark").val();
        var taskStatus = 0;
        layer.confirm('是否要下达任务？', function (index) {
            //do something
            addTask(taskStyle, batch, productlineNum, planQuantity, remark, taskStatus);
            layer.close(index);
            // window.location.reload();
        });


    });


    /**
     删除任务
     **/
    function delTask(taskStyle, batch, productlineNum) {
        $.ajax({
            // url: '../json/getTask.json',
            url: baseUrl + 'delTask',
            type: 'get',
            // dataType: 'default: Intelligent Guess (Other values: xml, json, script, or html)',
            data: {
                taskStyle: taskStyle,
                batch: batch,
                productlineNum: productlineNum
            },
        })
            .done(function (data) {
                console.log("delTask");
                if (data.code == 0) {
                    layer.open({
                        title: '信息'
                        , content: '任务删除成功！',
                        yes: function (index, layero) {
                            //按钮【按钮一】的回调
                            layer.close(index);
                            window.location.reload();
                        }
                    });
                } else if (data.code == 1) {
                    layer.open({
                        title: '信息'
                        , content: '任务删除失败！',
                        yes: function (index, layero) {
                            //按钮【按钮一】的回调
                            layer.close(index);
                            window.location.reload();
                        }
                    });
                }
            })
            .fail(function () {
                console.log("error");
                layer.open({
                    title: '信息'
                    , content: '任务删除失败！'
                });
            })
    }

    $("body").on('click', 'table .delTask', function (event) {
        event.preventDefault();
        /* Act on the event */
        var taskStyle = $(this).data("taskstyle");
        var batch = $(this).data("batch");
        var productlinenum = Number($(this).data("productlinenum"));

        layer.confirm('是否要删除任务？', function (index) {
            //do something
            delTask(taskStyle, batch, productlinenum);

            layer.close(index);
            // window.location.reload();
        });

    });


    /**
     完成任务
     **/
    function comTask(taskStyle, batch, productlineNum) {
        $.ajax({
            // url: '../json/getTask.json',
            url: baseUrl + 'comTask',
            type: 'get',
            // dataType: 'default: Intelligent Guess (Other values: xml, json, script, or html)',
            data: {
                // method: 'comTask',
                taskStyle: taskStyle,
                batch: batch,
                productlineNum: productlineNum
            },
        })
            .done(function (data) {
                console.log("comTask");
                if (data.code == 0) {
                    layer.open({
                        title: '信息'
                        , content: '操作成功！',
                        yes: function (index, layero) {
                            //按钮【按钮一】的回调
                            layer.close(index);
                            window.location.reload();
                        }
                    });
                } else if (data.code == 1) {
                    layer.open({
                        title: '信息'
                        , content: '操作失败！',
                        yes: function (index, layero) {
                            //按钮【按钮一】的回调
                            layer.close(index);
                            window.location.reload();
                        }
                    });
                }
            })
            .fail(function () {
                console.log("error");
                layer.open({
                    title: '信息'
                    , content: '操作失败！',
                    yes: function (index, layero) {
                        //按钮【按钮一】的回调
                        layer.close(index);
                        window.location.reload();
                    }
                });
            })
    }

    $("body").on('click', 'table .comTask', function (event) {
        event.preventDefault();
        /* Act on the event */
        var taskstyle = $(this).data("taskstyle");
        var batch = $(this).data("batch");
        var productlinenum = Number($(this).data("productlinenum"));
        layer.confirm('是否要完成任务？', function (index) {
            //do something
            comTask(taskstyle, batch, productlinenum);
            layer.close(index);
        });

    });

})